# -*- coding: utf-8 -*-
"""
****************************
@Time    : 2021/1/20 16:35
@Author  : Candy
@File    : my_logger.py
****************************
"""
import logging


def create_logger(prefix, log_file):
    """
    创建日志对象，调试阶段可以使用
    :return:
    """
    print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
    logger = logging.getLogger(prefix)
    logger.setLevel(logging.DEBUG)
    # 建立一个filehandler来把日志记录在文件里，级别为debug以上
    fh = logging.FileHandler(log_file, encoding="utf-8", mode='a')
    fh.setLevel(logging.DEBUG)
    # 建立一个streamhandler来把日志打在CMD窗口上，级别为debug以上
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    # 设置日志格式
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    ch.setFormatter(formatter)
    fh.setFormatter(formatter)
    # 将相应的handler添加在logger对象中
    logger.addHandler(ch)
    logger.addHandler(fh)
    return logger


def single(cls):

    instance = {}

    def wapper(*args, **kwargs):
        if cls not in instance:
            instance[cls] = cls(*args, **kwargs)
        
        return instance[cls]

    return wapper